import type { PostLoginData } from '@/types/common'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore(
  'user',
  () => {
    const token = ref('')
    // 设置 token
    const setToken = (value: string) => {
      token.value = value
    }
    // 清空 token
    const clearToken = () => {
      token.value = ''
    }
    // 用户信息
    const userInfo = ref<PostLoginData>()

    const setUserInfo = (value: PostLoginData) => {
      userInfo.value = value
    }

    const clearUserInfo = () => {
      userInfo.value = undefined
    }

    return { token, setToken, clearToken, userInfo, setUserInfo, clearUserInfo }
  },
  {
    // 持久化
    persist: true,
  }
)
